package com.jackchan.homework.sysw.web;

import com.jackchan.homework.common.web.BaseController;
import com.jackchan.homework.sysw.entity.Material;
import com.jackchan.homework.sysw.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by ch-hui on 15/12/20.
 */
@Controller
@RequestMapping("material")
public class MaterialController extends BaseController {

    private static final String BIZ_BASE = "sysw/material/";

    @Autowired
    private MaterialService materialService;
    /**
     * 默认页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView(BIZ_BASE + "material_list");
        mv.addObject("materials",materialService.findAllDto());
        return mv;
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable Long id){
        ModelAndView mv = new ModelAndView(BIZ_BASE + "material_form");
        Material material = materialService.findOne(id);
        if(material != null){
            mv.addObject("material", material);
            mv.addObject("nutritive", materialService.findByMaterial(material));
        }
        return mv;
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView(BIZ_BASE + "material_form");
        return mv;
    }
}
